package com.java.schedule.springbatch;

import com.java.schedule.springbatch.entity.EmployeeFrom;
import com.java.schedule.springbatch.entity.EmployeeTo;
import com.java.schedule.springbatch.job.JobRunner;
import com.java.schedule.springbatch.mapper.from.EmployeeFromMapper;
import com.java.schedule.springbatch.mapper.to.EmployeeToMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*
url: http://localhost:8888/run/job
 */

@RestController
@RequestMapping("/run")
public class JobController {

    private JobRunner jobRunner;

    @Resource
    private EmployeeFromMapper employeeFromMapper;

    @Resource
    private EmployeeToMapper employeeToMapper;

    @Autowired
    public JobController(JobRunner jobRunner) {
        this.jobRunner = jobRunner;
    }

    /**
     * http://localhost:8888/run/job
     * @return
     */
    @GetMapping("/job")
    public String runJob() {
        jobRunner.runBatchJob();
        return String.format("Job Demo4 submitted successfully.");
    }

    /**
     * 数据库 my
     * http://localhost:8888/run/get
     * @return
     */
    @GetMapping("/get")
    public List<EmployeeFrom> get(){
        List<EmployeeFrom> employeeFroms = employeeFromMapper.selectByPaging(1, 2);
        return employeeFroms;
    }

    /**
     * 数据库 my2
     * http://localhost:8888/run/insert
     * @return
     */
    @GetMapping("/insert")
    public int insert(){
        EmployeeTo e = new EmployeeTo();
        e.setAge(666);
        EmployeeTo e2 = new EmployeeTo();
        e2.setAge(999);
        ArrayList list = new ArrayList();
        list.add(e);
        list.add(e2);
        int i = employeeToMapper.insertByBatch(list);
        return i;
    }
}
